package com.dayuan.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * 数据库工具类
 */
public class DBUtil {

    private static SqlSessionFactory sqlSessionFactory ;


    public static void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        DBUtil.sqlSessionFactory = sqlSessionFactory;
    }

    /**
     * 获取数据库连接
     *
     * @return
     */
    public static SqlSession getConn(boolean isAutoCommit) {
        return sqlSessionFactory.openSession(isAutoCommit);
    }


    /**
     * 通用的close方法
     *
     * @param objs
     */
    public static void close(AutoCloseable... objs) {
        for (AutoCloseable obj : objs) {
            try {
                if (obj != null) {
                    obj.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
